package com.example.administrator.appstpremvpdemo.view.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.administrator.appstpremvpdemo.R;
import com.example.administrator.appstpremvpdemo.common.Constances;
import com.example.administrator.appstpremvpdemo.common.MainApplication;
import com.example.administrator.appstpremvpdemo.helper.SplashImageHelper;
import com.example.administrator.appstpremvpdemo.helper.SplashResBlockHelper;
import com.example.administrator.appstpremvpdemo.view.activity.LaunchActivity;
import com.example.administrator.appstpremvpdemo.view.activity.MainActivity;


/**
 * @Author Administrator
 * @Date 2017/12/12
 * @description
 * @function
 */

public class LaunchDefaultFragment extends LaunchBaseFragment {

    private ImageView mGuangGaoImg;
    private LaunchActivity mActivity;
    private SplashImageHelper mSplashImageHelper;
    private Handler mHandler;
    private splashhandler mSplashRunable;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentview = inflater.inflate(R.layout.launchdefaultfragment, container, false);
        mGuangGaoImg = ((ImageView) contentview.findViewById(R.id.guanggaoImage));
        return contentview;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mActivity = null;
        //当还没来得及进入首页就点击了返回键
        mHandler.removeCallbacks(mSplashRunable);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mActivity = (LaunchActivity) getActivity();
//        mSplashImageHelper = new SplashImageHelper(mActivity);
//        mSplashImageHelper.setIGetSplashImage(this);
//        mSplashImageHelper.preLoadSpashImage();
        MainApplication.getInstance().requsetSplashData();
        mHandler = new Handler();
        mSplashRunable = new splashhandler();
        //临时用Glide加载广告图，正式项目要自己从缓存实体中去拿图片的缓存url,然后用glide加载也行，
        Glide.with(mActivity).load(Constances.URL_SPALASH_IMG).centerCrop().listener(new RequestListener<String, GlideDrawable>() {

            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                return false;
            }

            //加载完成
            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                Toast.makeText(mActivity, "两秒后进入首页", Toast.LENGTH_SHORT).show();
                mHandler.postDelayed(mSplashRunable, 2000);
                return false;
            }
        }).into(mGuangGaoImg);

    }


    class splashhandler implements Runnable {
        public void run() {
            mILaunchCommplete.onLaunchCommplete(MainActivity.class);
        }
    }

    /**
     * 闪屏页信息实体类
     */
    public static class Entity {
        Bitmap mBitmap;
        boolean isSystemImageRes;
        SplashResBlockHelper mSplashResBlockHelper;

        public Entity() {
        }

        public Entity(Bitmap bitmap, boolean isSystemImageRes, SplashResBlockHelper splashResBlockHelper) {
            mBitmap = bitmap;
            this.isSystemImageRes = isSystemImageRes;
            mSplashResBlockHelper = splashResBlockHelper;
        }

        /**
         * 创建实体
         *
         * @param bitmap
         * @param isSystemImageRes
         * @param splashResBlockHelper
         * @return 闪屏页信息实体
         */
        public static Entity createEntity(Bitmap bitmap, boolean isSystemImageRes, SplashResBlockHelper splashResBlockHelper) {
            return new Entity(bitmap, isSystemImageRes, splashResBlockHelper);
        }
    }
}
